package com.acgist.snail.java11;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class NetworkInterfaceJava11 {
    public static Stream<NetworkInterface> networkInterfaces() throws SocketException {

        List<NetworkInterface> networkInterfaceList = new ArrayList<>();
        Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()) {
            networkInterfaceList.add(networkInterfaceEnumeration.nextElement());
        }
        return streamFromArray(networkInterfaceList.toArray(new NetworkInterface[networkInterfaceList.size()]));
    }

    private static <T> Stream<T> streamFromArray(T[] a) {
        return StreamSupport.stream(
                Spliterators.spliterator(
                        a,
                        Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL),
                false);
    }
}
